package cn.pegasus.teamwork.appinterface.controller.user;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import cn.iyizhan.teamwork.base.util.ObjectUtil;
import cn.pegasus.teamwork.appinterface.base.BaseController;
import cn.pegasus.teamwork.appinterface.controller.user.vo.cet.AccountVo;
import cn.pegasus.teamwork.appinterface.controller.user.vo.cet.CouponVo;
import cn.pegasus.teamwork.appinterface.controller.user.vo.cet.ReturnCollectionAreaVo;
import cn.pegasus.teamwork.appinterface.util.ParameterEunm;
import cn.pegasus.teamwork.appinterface.util.ReturnRsUtils;
import cn.pegasus.teamwork.pegasus.entity.BussAreaOllection;
import cn.pegasus.teamwork.pegasus.entity.BussCouponInfo;
import cn.pegasus.teamwork.pegasus.entity.BussUcenterArea;
import cn.pegasus.teamwork.pegasus.entity.BussUserCoupon;
import cn.pegasus.teamwork.pegasus.entity.BussUserFunddetails;
import cn.pegasus.teamwork.pegasus.entity.BussUserFundstatistics;
import cn.pegasus.teamwork.pegasus.entity.BussUserIntegraltotal;
import cn.pegasus.teamwork.pegasus.entity.BussUserOpinion;
import cn.pegasus.teamwork.pegasus.service.BussAreaOllectionService;
import cn.pegasus.teamwork.pegasus.service.BussCouponInfoService;
import cn.pegasus.teamwork.pegasus.service.BussUcenterAreaService;
import cn.pegasus.teamwork.pegasus.service.BussUserCouponService;
import cn.pegasus.teamwork.pegasus.service.BussUserFunddetailsService;
import cn.pegasus.teamwork.pegasus.service.BussUserFundstatisticsService;
import cn.pegasus.teamwork.pegasus.service.BussUserIntegraltotalService;
import cn.pegasus.teamwork.pegasus.service.BussUserOpinionService;

/**
 * @ClassName: SysUcenerUserController
 * @Description: 用户控制中心
 * @author ryan.bian
 * @date 2017年11月12日 上午10:10:32
 * 
 *       包括以下主要几个接口：
 * 
 *       =========================== 1.我的消息 2.我的账户 3.我的资金明细 4.我的优惠券 5.优惠券详情
 *       6.意见反馈 7.我的积分 地址模糊搜索 \我的地址收藏\签到接口
 * 
 *       =====================================
 * 
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@Controller
@RequestMapping("/cet/*")
public class UserCetInfoController extends BaseController {

	private static final Logger LOGGER = LoggerFactory.getLogger(UserCetInfoController.class);

	/**
	 * 账户资金总表
	 */
	@Autowired
	private BussUserFundstatisticsService fundstatisticsService;

	/**
	 * 账户明细列表
	 */
	@Autowired
	private BussUserFunddetailsService funddetailsService;

	/**
	 * 优惠券列表
	 */
	@Autowired
	private BussCouponInfoService couponInfoService;

	/**
	 * 我的优惠券
	 */
	@Autowired
	private BussUserCouponService userCouponService;
	
	@Autowired
	private BussAreaOllectionService areaOllectionService;
	
	@Autowired
	private BussUcenterAreaService areaService;

	/**
	 * 意见反馈
	 */
	@Autowired
	private BussUserOpinionService userOpinionService;
	
	@Autowired
	private BussUserIntegraltotalService integraltotalService;
		
	/**
	 * 我的账户
	 * 
	 * @param userName
	 * @param password
	 * @param app_code
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/acc", method = RequestMethod.POST)
	public Map<String, Object> account(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		map.put("objectId", getSessionUserId());
		try {
			AccountVo vo = new AccountVo();
			BussUserFundstatistics bus = fundstatisticsService.detail(map);
			if (bus != null) {
				vo.setBalance(bus.getBalance());
				vo.setAvailableBalance(bus.getAvailableBalance());
				vo.setRechargeBalance(bus.getRechargeBalance());
				vo.setGiveBalance(bus.getGiveBalance());
				Map<String, Object> tMap = new HashMap<String, Object>();
				tMap.put("userId", getSessionUserId());
				BussUserIntegraltotal bi = integraltotalService.detail(tMap);
				if(bi != null){
					vo.setIntegral(bi.getIntegralTotal());
				}else {
					vo.setIntegral(0);	
				}			
			
				resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE,vo);
			} else {
				resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE,null);
			}
		} catch (Exception e) {
			LOGGER.error("服务器异常!", e);
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_403_CODE, null);
		}
		return resultMap;
	}

	/**
	 * 我的资金明细
	 * 
	 * @param id
	 * @param map
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/det", method = RequestMethod.POST)
	public Map<String, Object> detail(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		map.put("objectId", getSessionUserId());
		try {
			List<BussUserFunddetails> bus = funddetailsService.query(map);
			if (bus != null) {
				Map<String, Object> mape = new HashMap<String, Object>();
            	mape.put("lis", bus);
				resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE,mape);
			} else {
				resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE,null);
			}
		} catch (Exception e) {
			LOGGER.error("服务器异常!", e);
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_403_CODE, null);
		}
		return resultMap;
	}
 
	/**
	 * 我的优惠券
	 * 
	 * @param phone_number
	 * @param app_code
	 * @return
	 */
	@RequestMapping(value = "/cs", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> coupons(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			map.put("userId", getSessionUserId());
			List<BussUserCoupon> buss = userCouponService.query(map);
			if (buss != null && buss.size() > 0) {
				List<CouponVo> bucss = new ArrayList<CouponVo>();
				for(BussUserCoupon buc : buss){
					CouponVo vo = new CouponVo();
					long id = buc.getCouponId();
					Map<String, Object> p = new HashMap<String, Object>();
					p.put("id", id);
					BussCouponInfo bu = couponInfoService.detail(p);
					if (bu != null){
						vo.setId(bu.getId());
						vo.setCouponName(bu.getCouponName());
						vo.setCcouponStatus(buc.getCcouponStatus());
						vo.setCouponCode(bu.getCouponCode());
						vo.setCouponDesc(bu.getCouponDesc());
						vo.setCouponImages(bu.getCouponImages());
						vo.setCouponPrice(bu.getCouponPrice());
//						vo.setValidityTimes(bu.getValidityTimes());
						
						bucss.add(vo);
					}
				}			
				Map<String, Object> mape = new HashMap<String, Object>();
            	mape.put("lis", bucss);
				resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE,mape);
			} else {
				resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE,null);
			}
		} catch (Exception e) {
			LOGGER.error("验证手机号码失败.", e);
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_500_CODE, null);
		}
		LOGGER.info("返回参数 ：{}", JSONObject.toJSON(resultMap));
		return resultMap;
	}

	/**
	 * 优惠券详情
	 * 
	 * @param phone_number
	 * @param app_code
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/cp", method = RequestMethod.POST)
	public Map<String, Object> coupon(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "id" }, map);
			if (isNotNull) {
				BussCouponInfo buss = couponInfoService.detail(map);
				if (buss != null) {
					resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE,buss);
				} else {
					resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE,null);
				}
			} else {
				resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE,null);
			}
		} catch (Exception e) {
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_500_CODE, null);
		}

		return resultMap;
	}
	
	/**
	 * 优惠券列表
	 * 
	 * @param phone_number
	 * @param app_code
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/cpl", method = RequestMethod.POST)
	public Map<String, Object> couponList(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		List<BussCouponInfo> buss = couponInfoService.query(map);
		if (buss != null && buss.size() > 0) {
			Map<String, Object> mape = new HashMap<String, Object>();
        	mape.put("lis", buss);
			resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE,mape);
		} else {
			resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE,null);
		}
		return resultMap;
	}
	
	

	/**
	 * 添加反馈意见
	 * 
	 * @param phone_number
	 * @param app_code
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/op", method = RequestMethod.POST)
	public Map<String, Object> opinion(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "descs" }, map);
			if (isNotNull) {
				map.put("userId", getSessionUserId());
				BussUserOpinion buss = userOpinionService.add(map);
				if (buss != null) {
					resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE,buss);
				} else {
					resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE,null);
				}
			} else {
				resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE,null);
			}
		} catch (Exception e) {
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_500_CODE, null);
		}

		return resultMap;
	}
	/**
	 * 添加地址收藏
	 * 
	 * @param phone_number
	 * @param app_code
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/ct", method = RequestMethod.POST)
	public Map<String, Object> collection(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "areaId" }, map);
			if (isNotNull) {
				map.put("userId", getSessionUserId());
				map.put("areaId",map.get("areaId"));								
				map.put("washerStatus", 2);
				BussAreaOllection buss = areaOllectionService.add(map);
				if (buss != null) {
					resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE,buss);
				} else {
					resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE,null);
				}
			} else {
				resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE,null);
			}
		} catch (Exception e) {
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_500_CODE, null);
		}

		return resultMap;
	}
	
	/**
	 * 添加地址收藏
	 * 
	 * @param phone_number
	 * @param app_code
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/qcl", method = RequestMethod.POST)
	public Map<String, Object> queryCollectionList(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			map.put("userId", getSessionUserId());
			List<BussAreaOllection> buss = areaOllectionService.query(map);
			if (buss != null && buss.size() > 0) {
				List<ReturnCollectionAreaVo> vos = new ArrayList<ReturnCollectionAreaVo>();
				for(BussAreaOllection bus : buss){
					int areaId = bus.getAreaId();
					Map<String, Object> tMap = new HashMap<String, Object>();
					map.put("userId", getSessionUserId());
					map.put("id",areaId);
					BussUcenterArea area = areaService.detail(tMap);
					ReturnCollectionAreaVo vo = new ReturnCollectionAreaVo();
					vo.setAreaName(area.getName());
					vo.setId(area.getId().toString());
					vos.add(vo);
				}
				
				Map<String, Object> mape = new HashMap<String, Object>();
	        	mape.put("lis", vos);
				resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE,buss);				
			} else {
				resultMap =  ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE,null);
			}
		} catch (Exception e) {
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_500_CODE, null);
		}

		return resultMap;
	}
}
